package com.zzg.config;

import java.util.List;

import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.zzg.api.JobConfigService;
import com.zzg.entity.JobConfig;
import com.zzg.util.SchedulerUtil;
/**
 * 
 * @ClassName:  SchedulerConfig   
 * @Description: 定时任务配置--项目启动服务是否加载   
 * @author: 世纪伟图 -zzg
 * @date:   2018年12月19日 下午3:10:52   
 *     
 * @Copyright: 2018 www.digipower.cn 
 * 注意：本内容仅限于深圳市世纪伟图科技开发有限公司内部使用，禁止用于其他的商业目的
 */
@Configuration
@ConditionalOnExpression("'${quartz.enabled}'=='true'")
public class SchedulerConfig {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ApplicationContext applicationContext;

	@Autowired
	private JobConfigService jobConfigService;

	@Bean
	public StdSchedulerFactory stdSchedulerFactory() {
		StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
        //获取JobConfig集合
        List<JobConfig> configs = jobConfigService.selectAll();
        logger.debug("Setting the Scheduler up");
        for (JobConfig config : configs) {
            try {
                Boolean flag = SchedulerUtil.createScheduler(config, applicationContext);
                System.out.println("执行结果：" + (flag == true ? "成功" : "失败"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
		return stdSchedulerFactory;
	}

}
